package br.com.monografia.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import br.com.monografia.model.Alimento;
import br.com.monografia.util.ConnectDatabase;

public class AlimentoDao {
	

	protected SQLiteDatabase db;
	protected ConnectDatabase helper;

	public AlimentoDao(Context ctx){
		helper = new ConnectDatabase(ctx);
		db = helper.getDatabase();	
	}
	
	
	public List<Alimento> listar() {
		List<Alimento> alimentos = new ArrayList<Alimento>();
		try {
			Cursor c = db.rawQuery("SELECT * FROM Alimentos ORDER BY descricao", null);
			c.moveToFirst();			
			do  {		
				Alimento alimento = new Alimento();
				alimento.setId_alimentos(c.getInt(c.getColumnIndex("id_alimentos")));
				alimento.setClasse(c.getString(c.getColumnIndex("classe")));
				alimento.setDescricao(c.getString(c.getColumnIndex("descricao")));
				alimento.setMedida(c.getString(c.getColumnIndex("medida")));
				alimento.setPeso(c.getInt(c.getColumnIndex("peso")));
				alimento.setCarboidrato(c.getInt(c.getColumnIndex("carboidrato")));
				alimentos.add(alimento);	
			} while (c.moveToNext());
			c.close();
			db.close();			
		} catch (Exception e) {
			Log.e("ALimentoDao", "Erro ao buscar alimentos" + e.toString());
		}
		return alimentos;
	}
	
	public List<String> getClasses() {
		List<String> classes = new ArrayList<String>();
		try {
			Cursor c = db.rawQuery("SELECT DISTINCT descricao FROM alimentos", null);
			c.moveToFirst();
			do  {	
				String classe = new String();
				classe = c.getString(c.getColumnIndex("descricao"));
				classes.add(classe);				
			} while (c.moveToNext());
			c.close();
			db.close();			
		} catch (Exception e) {
			Log.e("ALimentoDao", "Erro ao buscar Classes" + e.toString());
		}
		return classes;
	}

}	


